package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-12-25
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    private static final Logger logger = LoggerFactory.getLogger(EduVideoServiceImpl.class);
    @Autowired
    private VodClient vodClient;
    /**
     * 根据课程id删除小结
     * @param courseId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeVideoByCourseId(String courseId) {

            /*LambdaQueryWrapper<EduVideo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(EduVideo::getCourseId,courseId);
            List<EduVideo> list = this.list(queryWrapper);
            for (EduVideo eduVideo : list) {
                removeVideosById(eduVideo.getId());
            }*/
        LambdaQueryWrapper<EduVideo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduVideo::getCourseId,courseId);
        queryWrapper.select(EduVideo::getVideoSourceId);//只查找一个字段提高效率
        List<EduVideo> list = this.list(queryWrapper);
        list.forEach(eduVideo ->  logger.info("遍历list:{}",eduVideo));
        List<String> ids = list.stream()
                .filter(s -> org.springframework.util.StringUtils.hasText(s.getVideoSourceId())==true)
                .map(EduVideo::getVideoSourceId)
                .collect(Collectors.toList());
        logger.info("ids为：{}",ids);
        if (ids.size()>0){
            vodClient.deleteAlyVideos(ids);
        }

        //删除video表示的记录
        QueryWrapper<EduVideo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("course_id", courseId);
        Integer count = baseMapper.delete(queryWrapper2);
        return null != count && count > 0;
    }

    /**根据小结id删除小结以及视频*/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeVideosById(String videoId) {
        try {
            EduVideo eduVideo = this.getById(videoId);
            logger.info("获取到的小结对象为：{}",eduVideo);
            String videoSourceId = eduVideo.getVideoSourceId();
            logger.info("收到的id为：{}",videoSourceId);
            if(!StringUtils.isEmpty(videoSourceId)){
                vodClient.deleteAlyVideo(videoSourceId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001,"删除阿里云视频失败");
        }
        baseMapper.deleteById(videoId);
    }
}
